﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ClientLib.Common;
using ClientLib.MonitorState2;
using ClientLib.Logic;

namespace WpfDataBingding.UserDefControl
{
    /// <summary>
    /// DynamicMonitor2.xaml 的交互逻辑
    /// </summary>
    public partial class DynamicMonitor2 : UserControl
    {
        public DynamicMonitor2()
        {
            InitializeComponent();
        }

         public DynamicMonitor2(DeviceDynamic device)
        {
            InitializeComponent();
            this.Monitor_lay.DataContext = device;
            this.Monitor_lay.Uid = device.Device_ID + ":" + device.Monitor_ID;
        }

        private void BtChange_Click(object sender, RoutedEventArgs e)
         {

             #region
             Button bt_change = sender as Button;


            ////////根据datacontext获取UI元素
            DynamicState device = (DynamicState)bt_change.DataContext;
            ListViewItem viewitem= MonitorView.ItemContainerGenerator.ContainerFromItem(device) as ListViewItem;
            /////////////

            //////////// 查找类容UI中元素
            ContentPresenter[] cplist = FindVisualChild<ContentPresenter>(viewitem);
            foreach (var cp in cplist)
            {
                Label labshow = (Label)cp.ContentTemplate.FindName("LabShowValue", cp);
                if (labshow != null)
                {
                    labshow.Visibility = Visibility.Hidden;
                    labshow.Height = 0;
                }
       


                TextBox txtValue = (TextBox)cp.ContentTemplate.FindName("TxtNewValue", cp);
                if (txtValue != null)
                {
                    txtValue.Visibility = Visibility.Visible;
                    txtValue.Height = 32;
                }

                Button butchange = (Button)cp.ContentTemplate.FindName("BtChange", cp);
                if (butchange != null)
                {
                    butchange.Visibility = Visibility.Hidden;
                    butchange.Height=0;

                }

                Button butconfirm = (Button)cp.ContentTemplate.FindName("BtConfirm", cp);
                if (butconfirm!=null)
                {
                    butconfirm.Visibility = Visibility.Visible;
                    butconfirm.Height=29;
                }
                Button butcancel = (Button)cp.ContentTemplate.FindName("BtCancel", cp);
                if (butcancel != null)
                {
                    butcancel.Visibility = Visibility.Visible;
                    butcancel.Height = 29;
                }
            }
             #endregion

         }

        /// <summary>
        /// 获取类容UI元素，网上查找
        /// </summary>
        /// <typeparam name="childItem"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        private childItem[] FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            List<childItem> item = new List<childItem>();
            #region
            /*
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
             */
            #endregion

            #region  ////获取多个childrenitem ，UI中有多个模板
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    item.Add(child as childItem);
                else
                {
                    childItem[] childOfChild = FindVisualChild<childItem>(child);
                    foreach (var children in childOfChild)
                        item.Add(children);
                }
            }
            #endregion
            return item.ToArray();
        }

 

        private void BtConfirm_Click(object sender, RoutedEventArgs e)
        {
            Button bt_change = sender as Button;
            DynamicState devicestate = (DynamicState)bt_change.DataContext;

            #region
 
            ////////根据datacontext获取UI元素
            ListViewItem viewitem = MonitorView.ItemContainerGenerator.ContainerFromItem(devicestate) as ListViewItem;
            /////////////

            //////////// 查找类容UI中元素
            ContentPresenter[] cplist = FindVisualChild<ContentPresenter>(viewitem);
            TextBox txtValue = null ;
            foreach (var cp in cplist)
            {
                 txtValue = (TextBox)cp.ContentTemplate.FindName("TxtNewValue", cp);
                if (txtValue != null)
                {
                    break;
                }  
            }
            #endregion
            if (txtValue != null)
            {
                string strvalue = txtValue.Text.ToString();
               ChangeStateBackInfo backinfo=DynamicDeviceState.ChangeDeviceDynamicState(devicestate, strvalue);
               if (backinfo.isSucess)
               {
                   MessageBox.Show("属性修改成功！");
               }
               else
               {
                   MessageBox.Show(backinfo.info);
               } 
            }
            else
            {
                MessageBox.Show("修改属性值未获取！");
            }
        }

        private void BtCancel_Click(object sender, RoutedEventArgs e)
        {
            #region
            Button bt_change = sender as Button;
             

            ////////根据datacontext获取UI元素
            DynamicState device = (DynamicState)bt_change.DataContext;
            ListViewItem viewitem = MonitorView.ItemContainerGenerator.ContainerFromItem(device) as ListViewItem;
            /////////////

            //////////// 查找类容UI中元素
            ContentPresenter[] cplist = FindVisualChild<ContentPresenter>(viewitem);
            foreach (var cp in cplist)
            {
                Label labshow = (Label)cp.ContentTemplate.FindName("LabShowValue", cp);
                if (labshow != null)
                {
                    labshow.Visibility = Visibility.Visible;
                    labshow.Height = 32;
                }



                TextBox txtValue = (TextBox)cp.ContentTemplate.FindName("TxtNewValue", cp);
                if (txtValue != null)
                {
                    txtValue.Visibility = Visibility.Hidden;
                    txtValue.Height = 0;
                }

                Button butchange = (Button)cp.ContentTemplate.FindName("BtChange", cp);
                if (butchange != null)
                {
                    butchange.Visibility = Visibility.Visible;
                    butchange.Height=29;

                }

                Button butconfirm = (Button)cp.ContentTemplate.FindName("BtConfirm", cp);
                if (butconfirm != null)
                {
                    butconfirm.Visibility = Visibility.Hidden;
                    butconfirm.Height= 0;
                }
                Button butcancel = (Button)cp.ContentTemplate.FindName("BtCancel", cp);
                if (butcancel != null)
                {
                    butcancel.Visibility = Visibility.Hidden;
                    butcancel.Height = 0;
                }
            }
            #endregion
        }
    }
}
